package com.hanxu.o2o.web.superadmin;

import com.hanxu.o2o.entity.Area;
import com.hanxu.o2o.service.AreaService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author hanjinge
 * @date 2022/4/28
 */
@Controller
@RequestMapping("/superadmin")
public class AreaController {

    //实例化一个logback.xml的日志功能
    Logger logger = LoggerFactory.getLogger(AreaController.class);

    @Autowired
    private AreaService areaService;

    //ResponseBody返回json数据
    @ResponseBody
    @RequestMapping(value = "/listarea",method = RequestMethod.GET)
    private Map<String, Object> listArea(){
        logger.info("============start=============");
        //获取执行的时间
        long startTime = System.currentTimeMillis();

        //hashmap k不能重复  把结果存到map中
        Map<String, Object> modelMap = new HashMap<String, Object>();
        List<Area> list = new ArrayList<>();
        try {
            list = areaService.getAreaList();
            //正确就返回结果的集合
            modelMap.put("rows",list);
            //返回正确结果的行数
            modelMap.put("total",list.size());
        }catch (Exception e){
            logger.error(e.toString());
            //异常走这里
            e.printStackTrace();
            modelMap.put("succes",false);
            //返回错误信息
            modelMap.put("errMsg",e.toString());
        }


        long endtTime = System.currentTimeMillis();
        logger.debug("costTime:[{}ms]",endtTime - startTime);
        logger.info("============end=============");
     return modelMap;
    }

}
